package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.core.entity.Output;
import com.ruoyi.core.util.IdUtil;
import com.ruoyi.web.domain.SysMod;
import com.ruoyi.web.domain.SysModColumn;
import com.ruoyi.web.exception.ApiException;
import com.ruoyi.web.mapper.SysModColumnMapper;
import com.ruoyi.web.mapper.SysModMapper;
import com.ruoyi.web.service.SysModColumnService;
import com.ruoyi.web.service.SysModDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class SysModColumnServiceImpl extends BaseService<SysModColumnMapper, SysModColumn, SysModColumn> implements SysModColumnService {


    @Autowired
    private SysModColumnMapper sysModColumnMapper;

    @Autowired
    private SysModMapper sysModMapper;

    @Autowired
    private SysModDbService sysModDbService;

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public SysModColumn add(SysModColumn entity) {


        entity.setColumnName(entity.getColumnName().toLowerCase());

        SysModColumn oldColumn = getByColumnName(entity.getModId(), entity.getColumnName());
        if (oldColumn != null) {
            throw new ApiException("字段名不允许重复");
        }

        entity.setColumnId(IdUtil.sequenceId());
        save(entity);

        sysModDbService.addColumn(getMod(entity.getModId()), entity);


        return getById(entity);

    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public SysModColumn edit(SysModColumn entity) {

        entity.setColumnName(entity.getColumnName().toLowerCase());

        SysModColumn oldColumn = getById(entity.getColumnId());
        if (oldColumn == null || !oldColumn.getColumnName().equals(entity.getColumnName())) {
            throw new ApiException("字段名不允许修改");
        }

        updateById(entity);

        sysModDbService.editColumn(getMod(entity.getModId()), entity);

        return getById(entity);

    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Object delete(String columnId) {

        SysModColumn entity = getById(columnId);

        if ("1".equals(entity.getIsPrimaryKey())) {
            throw new ApiException("主键不能删除");
        }
        if ("1".equals(entity.getIsSystemColumn())) {
            throw new ApiException("固定列不能删除");
        }

        removeById(entity);

        sysModDbService.deleteColumn(getMod(entity.getModId()), entity);

        return Output.success();

    }

    @Override
    public SysModColumn getByColumnName(String modId, String columnName) {
        return sysModColumnMapper.selectVoOne(Wrappers.<SysModColumn>query().lambda().eq(SysModColumn::getModId, modId).eq(SysModColumn::getColumnName, columnName));
    }

    @Override
    public List<SysModColumn> getList(String modId) {
        if (StringUtils.isEmpty(modId)) {
            throw new ApiException("模型ID不能为空");
        }

        return list(Wrappers.<SysModColumn>query().lambda().eq(SysModColumn::getModId, modId));
    }


    private SysMod getMod(String modId) {
        return sysModMapper.selectVoById(modId);
    }

}
